layui.define(["plus", "plusConfig"], function (exports) {
  "use static";
  var $ = layui.jquery,
    plus = layui.plus,
    config = layui.plusConfig;

  var lastTime = 0;

  var plusTools = {
    init: function () {
      plusTools.reg();
      setTimeout(function () {
        plusTools.menuEffect();
        plusTools.centerToggle();
      }, 100);
    },
    reg: function () {
      $(document).on("click", "*[plus-tools]", function () {
        var t = plusTools[$(this).attr("plus-tools")];
        t && t.call(this, $(this));
      });
      plusTools.serachBar();
    },
    searchMenus: function (o, p) {
      var mens = plusTools.getStore("menus");
      var obj = [];
      $.each(mens, function (index, item) {
        if (item.id === Number(o) || item.url === o) {
          obj = p === undefined ? item.subMenus : item;
        }
      });
      return obj;
    },
    getStore: function (key) {
      var cache = layui.data(config.cacheName);
      if (cache) return cache[key];
    },
    putStore: function (key, value) {
      layui.data(config.cacheName, {
        key: key,
        value: value,
      });
    },
    removeStore: function (key) {
      layui.data(config.cacheName, {
        key: key,
        remove: true,
      });
    },
    removeAll: function () {
      localStorage.clear();
    },
    serachBar: function () {
      $(document).on("click", ".form-expand,[expand-btn]", function () {
        var $this = $(this);
        var $form = $this.parents(".layui-form").first();
        var expand = $this.data("expand");
        var change = $this.attr("expand-btn");
        if (expand === undefined || expand === true) {
          expand = true;
          $this.data("expand", false);
          $this.html('<i class="layui-icon-up" title="收起"></i>');
          var $elem = $form.find(".form-expand-show");
          $elem.attr("expand-show", "");
          $elem.removeClass("form-expand-show");
        } else {
          expand = false;
          $this.data("expand", true);
          $this.html('<i class="layui-icon-down" title="展开"></i>');
          $form.find("[expand-show]").addClass("form-expand-show");
        }
        plus.resizeTableHeight();
        if (!change) return;
        new Function("expand", change)({ expand: expand, elem: $this });
      });
    },
    menuEffect: function () {
      $(".menu-item").mouseover(function () {
        if ($(".plus-admin-menu.fold").length > 0) {
          $(this).find(".menu-item-tips").show();
        }
      });
      $(".menu-item").mouseout(function () {
        if ($(".plus-admin-menu.fold").length > 0) {
          $(this).find(".menu-item-tips").hide();
        }
      });
    },
    menuToggle: function () {
      if ($(".plus-admin-menu").hasClass("open")) {
        $(".header-logo").toggleClass("open");
        $(".plus-admin-menu").toggleClass("open");
        $(".plus-admin-menu").addClass("fold");
        $(this).text("➡️");
        if (
          $(".plus-admin-submenu").hasClass("fold") ||
          $(".plus-admin-submenu").css("display") === "none"
        ) {
          $(".plus-admin-submenu").css({ left: "-70px" });
          $(".plus-admin-body").css({ left: "50px" });
        } else {
          $(".plus-admin-submenu").css({ left: "50px" });
          $(".plus-admin-body").css({ left: "160px" });
        }
      } else {
        $(".header-logo").toggleClass("open");
        $(".plus-admin-menu").toggleClass("open");
        $(".plus-admin-menu").removeClass("fold");
        $(this).text("⬅️");
        if (
          $(".plus-admin-submenu").hasClass("fold") ||
          $(".plus-admin-submenu").css("display") === "none"
        ) {
          $(".plus-admin-submenu").css({ left: "-8px" });
          $(".plus-admin-body").css({ left: "115px" });
        } else {
          $(".plus-admin-submenu").css({ left: "115px" });
          $(".plus-admin-body").css({ left: "225px" });
        }
      }
      plus.resizeTable();
    },
    fixedToggle: function () {
      debugger;
      var left = 0,
        subleft = 0;
      if ($(this).hasClass("fold")) {
        $(".plus-admin-submenu").addClass("open").removeClass("fold");
        $(this).toggleClass("fold");
        if ($(".plus-admin-menu").hasClass("fold")) {
          if (config.topMenu) {
            left = 110;
            subleft = 0;
          } else {
            left = 160;
            subleft = 50;
          }
        } else {
          left = 225;
          subleft = 115;
        }
      } else {
        $(".plus-admin-submenu").addClass("fold").removeClass("open");
        $(this).toggleClass("fold");
        if ($(".plus-admin-menu").hasClass("fold")) {
          if (config.topMenu) {
            left = 0;
            subleft = -120;
          } else { 
            left = 50;
            subleft = -70;
          }
        } else {
           left = 115;
           subleft = -8;
        }
      }
      $(".plus-admin-body").css("left", left + "px");
      $(".plus-admin-submenu").css("left", subleft + "px");
      plus.resizeTable();
    },
    centerToggle: function () {
      if (config.topMenu) {
        $(".user-center").mouseenter(function (e) {
          $(".user-card")
            .css("top", $(this).height() - 2 + "px")
            .css(
              "left",
              $(this).offset().left + ($(this).outerWidth() - 88) / 2
            )
            .removeClass("bottom");
          $(".user-card").css("display", "block");
        });
      } else {
        $(".user-center").mouseenter(function (e) {
          $(".user-card")
            .css("left", $(this).width() - 5 + "px")
            .css("bottom", "15px");
          $(".user-card").css("display", "block");
        });
      }
      $(".user-card").mouseleave(function () {
        $(this).css("display", "none");
      });
    },
    drawerToggle: function () {
      if ($(".plus-admin-panel").hasClass("fold")) {
        $(this)
          .find("i")
          .removeClass("layui-icon-chat")
          .addClass("layui-icon-close");
        $(".plus-admin-panel").toggleClass("fold");
        $(".plus-admin-body").css({ right: "160px" });
      } else {
        $(this)
          .find("i")
          .removeClass("layui-icon-close")
          .addClass("layui-icon-chat");
        $(".plus-admin-panel").toggleClass("fold");
        $(".plus-admin-body").css({ right: "0" });
      }
      plus.resizeTable();
    },
    foldMenu: function () {
      plus.foldMenu();
    },
    refresh: function () {
      plus.refresh();
      plus.showLoading({ elem: this, type: "btn" });
      setTimeout(() => {
        plus.hideLoading({ elem: this, fade: false, type: "btn" });
      }, 600);
    },
    back: function () {
      $(this).data("window").history.back();
    },
    leftPage: function () {
      plus.rollPage("left");
    },
    rightPage: function () {
      plus.rollPage();
    },
    closeThisTabs: function () {
      var url = $(this).data("url");
      plus.closeThisTabs(url);
    },
    closeOtherTabs: function () {
      plus.closeOtherTabs();
    },
    closeAllTabs: function () {
      plus.closeAllTabs();
    },
    logout: function () {
      layer.confirm(
        "确定要退出登录吗？",
        { title: "温馨提示", shade: 0.1 },
        function () {
          config.removeToken && config.removeToken();
          location.replace("../plus-admin/login.html");
        }
      );
    },
  };
  plusTools.init(); //初始化
  exports("plusTools", plusTools);
});
